﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Student_Portal
{
    public partial class ViewStudent : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SqlConnection con = new SqlConnection();
                con.ConnectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
                con.Open();
                SqlCommand cmd = new SqlCommand("select Id,Name,Class,Rollno from Student_Info", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                con.Close();
                DataSet ds = new DataSet();
                da.Fill(ds);
                Session["MainDS"] = ds;
                grdDisplay.DataSource = ds;
                grdDisplay.DataBind();
            }
           
        }

        protected void grdDisplay_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblClass = (Label)e.Row.FindControl("lblClass");
                if (lblClass.Text == "1")
                    lblClass.Text = "First Std";
                if (lblClass.Text == "2")
                    lblClass.Text = "Second Std";
                if (lblClass.Text == "3")
                    lblClass.Text = "Third Std";
                if (lblClass.Text == "4")
                    lblClass.Text = "Fourth Std";
            }
        }

        protected void grdImage_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblPhoto = (Label)e.Row.FindControl("lblPhto");
                Image imgPhoto = (Image)e.Row.FindControl("imgPjhoto");
                imgPhoto.ImageUrl = "~/Images/" + lblPhoto.Text;
            }
        }

        protected void GrdView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblClass = (Label)e.Row.FindControl("lblClass");
                if (lblClass.Text == "1")
                    lblClass.Text = "First Std";
                if (lblClass.Text == "2")
                    lblClass.Text = "Second Std";
                if (lblClass.Text == "3")
                    lblClass.Text = "Third Std";
                if (lblClass.Text == "4")
                    lblClass.Text = "Fourth Std";
            }
        }

        protected void grdDisplay_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            HiddenField  hdn = (HiddenField )grdDisplay.Rows[e.NewSelectedIndex].FindControl("hdnId");
            string Id = hdn.Value;
            ViewState["Id"] = Id;
               MultiView1.SetActiveView(ViewmodeView);
               SqlConnection con = new SqlConnection();
               con.ConnectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
               con.Open();
               SqlCommand cmd = new SqlCommand("select Id,Name,Class,Rollno,Image from Student_Info where Id='" + ViewState["Id"] + "'", con);
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               con.Close();
               DataSet ds = new DataSet();
               da.Fill(ds);
               Session["DS"] = ds;
               grdImage.DataSource = ds;
               grdImage.DataBind();
               GrdView.DataSource = ds;
               GrdView.DataBind();
           
        }

        protected void MultiView1_ActiveViewChanged(object sender, EventArgs e)
        {
            if (MultiView1.GetActiveView().ID == "ViewmodeView")
            {
                SqlConnection con = new SqlConnection();
                con.ConnectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
                con.Open();
                SqlCommand cmd = new SqlCommand("select Id,Name,Class,Rollno,Photo from Student_Info where Id='" +   ViewState["Id"] + "'", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                con.Close();
                DataSet ds = new DataSet();
                da.Fill(ds);
                grdImage.DataSource = ds;
                grdImage.DataBind();
                GrdView.DataSource = ds;
                GrdView.DataBind();
            }
        }

        protected void GrdView_RowDataBound1(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblClass = (Label)e.Row.FindControl("lblClass1");
                if (lblClass.Text == "1")
                    lblClass.Text = "First Std";
                if (lblClass.Text == "2")
                    lblClass.Text = "Second Std";
                if (lblClass.Text == "3")
                    lblClass.Text = "Third Std";
                if (lblClass.Text == "4")
                    lblClass.Text = "Fourth Std";
            }
        }

        protected void GrdView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GrdView.EditIndex = e.NewEditIndex;
            BindData();
        }

        void BindData()
        {
            DataSet ds = (DataSet)Session["DS"];
            GrdView.DataSource = ds;
            GrdView.DataBind();
            DataSet mainds = (DataSet)Session["MainDS"];
            grdDisplay.DataSource = mainds;
            grdDisplay.DataBind();
        }

        protected void GrdView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = GrdView.Rows[e.RowIndex];
            string Name = ((TextBox)(row.Cells[0].Controls[0])).Text;
            //int Class = Convert.ToInt32(dlClass.SelectedValue);
            int RollNo = Convert.ToInt32(((TextBox)(row.Cells[2].Controls[0])).Text);
          
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            con.Open();

            SqlCommand cmd = new SqlCommand("update Student_Info set Name = @Name,Rollno = @Rollno where Id='" + ViewState["Id"].ToString() + "'", con);
            cmd.Parameters.AddWithValue("@Name", Name);
            cmd.Parameters.AddWithValue("@Rollno", RollNo);
          
            int ret = cmd.ExecuteNonQuery();
            
            GrdView.EditIndex = -1;
            cmd = new SqlCommand("select Id,Name,Class,Rollno from Student_Info where Id='" + ViewState["Id"] + "'", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            
            DataSet ds = new DataSet();
            da.Fill(ds);
            Session["DS"] = ds;
            GrdView.DataSource = ds;
            GrdView.DataBind();

            
            cmd = new SqlCommand("select Id,Name,Class,Rollno from Student_Info", con);
            da = new SqlDataAdapter(cmd);
            con.Close();
            ds = new DataSet();
            da.Fill(ds);
            Session["MainDS"] = ds;
            grdDisplay.DataSource = ds;
            grdDisplay.DataBind();
            //BindData();
            if (ret > 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "scrpt", "alert('Record updated Successfully!')", true);
                //Response.Redirect("ViewStudent.aspx");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "scrpt", "alert('Record updation Failed!')", true);
            }
        }

      
    }
}